package com.example.frn.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.frn.dto.AdminInsertDto;
import com.example.frn.dto.AdminUpdateDto;
import com.example.frn.dto.CategoryUpdateDTO;
import com.example.frn.dto.PageDTO;
import com.example.frn.entity.Admin;
import com.example.frn.entity.Category;
import com.example.frn.service.AdminService;
import com.example.frn.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @PostMapping("save")
    public R save(@RequestBody AdminInsertDto dto){
        Admin admin = BeanUtil.copyProperties(dto,Admin.class);
        if(adminService.save(admin)){
            return R.ok(true);
        }
        else return R.error(false);
    }
    @PostMapping("remove/{cid}")
    public R remove(@PathVariable("cid") Integer cid) {
        if (adminService.removeById(cid))
            return R.ok(true);
        else
            return R.error(false);
    }
    @PostMapping("update")
    public R update(@RequestBody AdminUpdateDto dto){
        Admin admin = BeanUtil.copyProperties(dto,Admin.class);
        if(adminService.updateById(admin)){
            return R.ok(true);
        }
        else return R.error(false);
    }
    @GetMapping("list")
    public R list(){
        return R.ok(adminService.list());
    }


    @GetMapping("detail/{cid}")
    public R detail(@PathVariable("cid") Integer cid){
        Admin admin = adminService.getById(cid);
        return R.ok(admin);
    }

    @PostMapping("page")
    public R page(@RequestBody PageDTO dto){
        return R.ok(adminService.page(dto.getPageNum(),dto.getPageSize()));
    }

}
